package com.cssy.xxkj.logic.web;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.contants.RoleUtil;
import com.cssy.xxkj.logic.entity.LogicProductionDispatchingDetailDO;
import com.cssy.xxkj.logic.entity.LogicProductionDispatchingDetailItemDO;
import com.cssy.xxkj.logic.service.ILogicProdDispatchingDetailService;
import com.cssy.xxkj.logic.util.OftenMothedUtil;
import com.cssy.yd.logic.util.SqlUtil;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;

/**
 * 生产派工取消详情
 * @author gzy
 *
 */
@Controller
@RequestMapping("/logic/prodetail")
public class ProductionDispatchingDetailController extends BaseController<LogicProductionDispatchingDetailDO>{

	private static final String MANAGER = "/xxkj/logic/productiondetail/ProDetailManager";// 列表页面
	private static final String EIDT = "/xxkj/logic/productiondetail/ProDetailOperation";// 添加、修改、显示页面
	
	@Resource(name = "logicProdDispatchingDetailService")
	private ILogicProdDispatchingDetailService logicProdDispatchingDetailService;
	
	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;
	
	@Resource(name = "oftenMothedUtil")
	private OftenMothedUtil oftenMothedUtil;
	
	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions dispScs = new SearchConditions();
		hander.getWheres(dispScs);
		dispScs=this.oftenMothedUtil.getNoFinishConditions(dispScs, "zd008WorkStatus", BillStatusUtil.PRODISPATDTL_BILL_STATUS_ZONE);
		if(StringUtils.isBlank(dispScs.getOrderBy())){
			dispScs.setOrderBy("bizCreatorDate desc");
		}
		//查询当前用户可以查看的车间编码
		Map<String,Object> sqlWSHMap = SqlUtil.sqlUtil.roleShSqlFilter(request, scUserRolePlaceService,"zd011WorkShopCode");
		dispScs.addCondition(sqlWSHMap.get("option")+"",(List<String>)sqlWSHMap.get("roleShList"));
		SearchResult result = logicProdDispatchingDetailService.findListByExample(dispScs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}

	@Override
	public String operationForm(HttpServletRequest request, @PathVariable String id,
			@PathVariable String flag, Model model) throws Exception {
		//是否是批量生成日报时查看
		String dailyEx = request.getParameter("dailyEx");
		if (OperationFlag.ADD.equals(flag)) {// 新增加时操作
			model.addAttribute("flag", "ADD");
		} else {
			LogicProductionDispatchingDetailDO prodetail = logicProdDispatchingDetailService.findListById(id);
			model.addAttribute("prodetail", prodetail);
		}
		if(StringUtils.isBlank(dailyEx)){
			model.addAttribute("buttonUp", "");
		}else{
			model.addAttribute("buttonUp", "yes");
		}
		boolean isCccj = SessionUtil.getUserExRole(RoleUtil.ROLW_CC_DT, request);
		if(isCccj){
			//表示是
			model.addAttribute("ccFlag", "yes");
		}else{
			//表示否
			model.addAttribute("ccFlag", "no");
		}
		return EIDT;
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicProductionDispatchingDetailDO curAP, BindingResult result,
			Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String itemdata = request.getParameter("itemdata");
			if (StringUtils.isBlank(itemdata)) {
				bodyMap.isFail();
				bodyMap.configWebInfor("产品列表不能为空！");
				return bodyMap.modelMap;
			}
			logicProdDispatchingDetailService.detailSave(request, curAP);
			bodyMap.isSuccess();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	@RequestMapping("/gridItems")
	public void gridItems(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("id");
		if(StringUtils.isNotBlank(id)){
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("detailId", id);
			List<LogicProductionDispatchingDetailItemDO> detailItemList = logicProdDispatchingDetailService.findItemListByMap(paramMap);
			if(detailItemList==null){
				detailItemList = new ArrayList<LogicProductionDispatchingDetailItemDO>();
			}
			List<LogicProductionDispatchingDetailItemDO> compItemList = new ArrayList<LogicProductionDispatchingDetailItemDO>();
			for(LogicProductionDispatchingDetailItemDO detailItem : detailItemList){
				if(detailItem==null||StringUtils.isBlank(detailItem.getId())){
					continue;
				}
				BigDecimal pgQuantity = detailItem.getZd013Quantity()==null?BigDecimal.ZERO:detailItem.getZd013Quantity();
				BigDecimal cancelQuantity = detailItem.getZd019ReturnQuantity()==null?BigDecimal.ZERO:detailItem.getZd019ReturnQuantity();
				BigDecimal cmQuantity = pgQuantity.subtract(cancelQuantity);
				if(cmQuantity.compareTo(BigDecimal.ZERO)==1){
					compItemList.add(detailItem);
				}
			}
			OutJsonGrid out = new OutJsonGrid(compItemList);
			out.printJSon(response);
		}
	}
}
